<script setup lang="ts">
import { ref } from 'vue'
import type { UploaderAfterRead } from 'vant/lib/uploader/types'
import { showToast } from 'vant'
import { uploadImg } from '@/services/consult'
import type { Image } from '@/types/consult'
defineProps<{
  disabled: boolean
}>()
const emits = defineEmits<{
  (e: 'onSendMsg', msgText: string): void
  (e: 'onSendImg', img: Image): void
}>()
const iptMsg = ref('')

const handleSendMsg = () => {
  emits('onSendMsg', iptMsg.value)
  iptMsg.value = ''
}

const afterRead: UploaderAfterRead = async (item) => {
  // 此时可以自行将文件上传至服务器
  if (Array.isArray(item)) return
  if (!item.file) return
  let toast = showToast({
    message: '正在上传...',
    duration: 0
  })
  // 上传图片请求
  let { data } = await uploadImg(item.file)
  // 发送图片消息
  emits('onSendImg', data)
  // 关闭toast
  toast.close()
}
</script>

<template>
  <div class="room-action">
    <van-field
      type="text"
      class="input"
      :border="false"
      placeholder="问医生"
      autocomplete="off"
      :disabled="disabled"
      v-model="iptMsg"
      @keyup.enter="handleSendMsg"
    ></van-field>
    <van-uploader
      :preview-image="false"
      :disabled="disabled"
      :after-read="afterRead"
    >
      <cp-icon name="consult-img" />
    </van-uploader>
  </div>
</template>

<style lang="scss" scoped>
.room-action {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 60px;
  background-color: #fff;
  display: flex;
  align-items: center;
  padding: 0 16px;
  z-index: 1;
  box-sizing: border-box;
  .input {
    background-color: var(--cp-bg);
    border: none;
    border-radius: 25px;
    margin-right: 10px;
    padding: 8px 15px;
  }
  .cp-icon {
    width: 24px;
    height: 24px;
  }
}
</style>
